<template>
  <div>
    <h2>影片库管理</h2>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-button type="primary" @click="gotoCreate">创建新影片</el-button>
      </el-form-item>
      <el-form-item label="影片名称:">
        <el-input v-model="keyword" placeholder="请输入影片名称关键字查询"></el-input>
      </el-form-item>
      <el-form-item>
        <i class="el-icon-search" @click="doSearch()" style="font-size:150%;font-weight:500;"></i>
      </el-form-item>
    </el-form>
    <el-table :data="movieList" stripe style="width: 100%" v-loading="tableLoading">
      <el-table-column prop="movieId" label="影片Id" width="80">
      </el-table-column>
      <el-table-column prop="name" label="影片名称">
      </el-table-column>
      <el-table-column label="影片海报">
        <template slot-scope="scope">
          <img :src="scope.row.img" class="movie-img-tablecell">
        </template>
      </el-table-column>
      <el-table-column prop="rating" label="综合评分">
        <template slot-scope="scope">
          {{parseFloat(scope.row.rating).toFixed(1)}}
        </template>
      </el-table-column>
      <el-table-column prop="personCount" label="参演人数">
      </el-table-column>
      <el-table-column prop="length" label="影片时长">
      </el-table-column>
      <el-table-column prop="moviePublishDate" label="上映日期">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="rowEdit(scope.row)" type="text">编辑</el-button>
          <el-button type="text" @click="rowDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="page-tool" background layout="prev, pager, next" :total="total"
                   :current-page="pageIndex+1" :page-size="pageSize" @current-change="pageChange">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      movieList: [],
      total: 0,
      pageIndex: 0,
      pageSize: 5,
      keyword: '',
      tableLoading: false
    }
  },
  methods: {
    loadMovieList() {
      this.tableLoading = true;
      this.$http({
        url: '/api/movies/movie-list',
        params: {
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          keyword: this.keyword
        },
        method: 'GET'
      }).then((response) => {
        if (response.status == 200) {
          this.movieList = response.data.content;
          this.total = response.data.totalElements;
        }
      }).finally(() => {
        this.tableLoading = false;
      })
    },
    doSearch() {
      this.pageIndex = 0;
      this.loadMovieList();
    },
    pageChange(e) {
      console.log("分页切换", e);
      let page = e - 1;
      this.pageIndex = page;
      this.loadMovieList();
    },
    rowDelete(movie) {
      console.log("删除影片按钮点击", movie);
      this.$confirm('影片[' + movie.name + ']将被删除, 是否继续?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: '/api/movies/admin/delete/' + movie.movieId,
          method: 'DELETE'
        }).then((result) => {
          if (result.status == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.loadMovieList()
          }
        })

      }).catch(() => {

      });
    },
    //跳转到创建影片功能，指定编辑模式
    rowEdit(movie) {
      console.log("编辑影片按钮点击", movie);
      this.$router.push({path:"/admin/create-movie",query:{"isEdit":true,"movieId":movie.movieId}})
    },
    //跳转到创建影片
    gotoCreate(){
      this.$router.push("/admin/create-movie")
    }
  },
  mounted() {
    this.loadMovieList()
  }
}
</script>

<style>
.movie-img-tablecell {
  width: 40px;
  height: 60px;
}

.page-tool {
  margin-top: 20px;
  text-align: right;
}
</style>
